<div class="row">
    <div class="col-lg-12">
        <h2 id="nav-tabs">Управление группами</h2>

        <ul class="nav nav-tabs" style="margin-bottom: 15px;">
            <? foreach($groups as $group): ?>
                <li class="">
                    <a data-toggle="tab" href="#<?= $group->id ?>"><?= $group->group_name ?></a>
                </li>
            <? endforeach ?>
        </ul>
        <div id="myTabContent" class="tab-content">
            <? foreach($groups as $group): ?>
                <div id="<?= $group->id ?>" class="tab-pane fade">
                    <b><?= $group->group_name ?></b>
                    <br/>
                    &nbsp;&nbsp;
                    <input type="checkbox" id="<?= $group->id ?>" class="access_users" <?= ($group->access_users == "Y") ? "checked=\"checked\"": "" ?> >
                    Доступ к пользователям:
                    <br/>
                    &nbsp;&nbsp;
                    <input type="checkbox" id="<?= $group->id ?>" class="access_group" <?= ($group->access_groups == "Y") ? "checked=\"checked\"": "" ?> >
                    Доступ к группам:
                    <br/><br/>
                    <button class="btn btn-default save" type="submit" id="<?= $group->id ?>">Сохранить</button>
                    <br/><br/>
                    <div class="alert alert-success alert-dismissable" id="<?= $group->id ?>" style="display: none">
                        Группа успешно обновлена!
                    </div>
                </div>
            <? endforeach ?>
        </div>

    </div>
</div><!-- /.row -->

<script type="text/javascript">
    $(function(){
        $(".save").click(function(){
            var id = $(this).attr('id');
            var access_users = 'N';
            var access_group = 'N';

            if ($("input.access_users[id='"+id+"']").is(":checked")) {
                access_users = 'Y';
            } else {
                access_users = 'N';
            }
            if ($("input.access_group[id='"+id+"']").is(":checked")) {
                access_group = 'Y';
            } else {
                access_group = 'N';
            }

            $.post('/ajax/updateGroup', {
                'groupId'       : id,
                'access_users'  :  access_users,
                'access_group'  : access_group
            }, function(result){
                $("div.alert[id="+id+"]").show();
                setTimeout(function(){ $("div.alert[id="+id+"]").hide() }, 2000);
            });
        });
    });
</script>